import Dialog from "../../miniprogram_npm/@vant/weapp/dialog/dialog";
import {toastInfo, updateTerminalState} from "../../utils/common";

let app = getApp();
Page({
    editFormClick() {
        wx.navigateTo({
            url: "/pages/workOrderForm/workOrderForm"
        })
    }, data: {
        terminal: undefined, users: [], userModalVis: false, selectedUserId: undefined,

        isBind: false,
        isQianDao: false,

    },
    onLoad: function (options) {
        let station = app.globalData.currentStation;
        this.setData({
            terminal: station
        })
        let that = this;
        if (station.operator === null) {
            Dialog.confirm({
                title: "标题",
                message: '是否绑定维护人员?只有绑定后才能进行签到和维护单填写!',
            }).then(async () => {
                // on confirm
                let userInfo = JSON.parse(wx.getStorageSync("userInfo"));
                console.log("userInfo", userInfo)

                let response = await updateTerminalState({
                    terminalId: station.terminalId,
                    cardId: station.terminalId,
                    terminalState: station.terminalState,
                    operatorId: userInfo.id,
                    baseStationId: station.baseStationId,
                    location: station.baseStation.location,
                    name: station.baseStation.name,
                    latitude: station.baseStation.latitude,
                    longitude: station.baseStation.longitude,
                });
                if (response.isSuccess) {
                    toastInfo("绑定成功!")
                    that.setData({isBind: true})
                }

            }).catch(() => {
                // on cancel
                that.setData({isBind: false})
            });

        } else {
            that.setData({isBind: true})
        }
    },
    // 用户 模态框 按钮 确认  事件
    userModalConfirm() {
        this.setData({userModalVis: false})
        toastInfo("绑定成功!")
    }, selectUserChange(event) {
        this.setData({
            selectedUserId: event.detail,
        });
    },

    // 点击签到
    qianDao() {
        wx.showLoading({mask: true})
        let that = this;
        wx.getLocation({
            type: 'wgs84',
            success(res) {
                const {
                    latitude, longitude
                } = res;
                // const {latitude: stationLatitude, longitude: stationLongitude} = that.data.terminal.baseStation;
                // if (Math.sqrt((stationLatitude - latitude) * (stationLatitude - latitude) + (stationLongitude - longitude) * (stationLongitude - longitude)) < 0.1) {
                //     toastInfo("签到成功")
                // } else {
                //     toastInfo("当前不在签到范围内!签到失败!")
                // }
                toastInfo("签到成功 ")
                that.setData({
                    isQianDao: true
                })
            },
            complete: () => {
                wx.hideLoading()
            }
        })

    },
});
// cname: "demoUser"
// email: "1063131391@qq.com"
// id: "83e0def7-d68b-4dbf-a5f1-e960b078b920"
// islive: 1
// level: 0
// name: "demoUser"
// phone: "17610776234"
// uname: "demoUser"
// updateTime: "2020-08-28 16:51:21"
